#ifndef NETMAIN_H
#define NETMAIN_H

#include <QObject>

class QTcpSocket;
class NetMain : public QObject
{
    Q_OBJECT
protected:
    explicit NetMain(QObject *parent = nullptr);

public:
    static NetMain *getObject();
    /**
     * @brief 登录
     * @param username
     * @param password
     */
    void userLogin(QString username, QString password);
    void userRegister(QString username, QString password);
    void getDeviceValue(int deviceTypeId, int deviceTypeIndex);

signals:
    /**
     * @brief 登录结果
     * @param re
     */
    void userLoginRe(bool re);
    void userRegisterRe(bool re, QString info);
    void deviceValue(int deviceTypeId, int deviceTypeIndex, double value);
    void deviceValue(int deviceTypeId, int deviceTypeIndex, bool value);
    void deviceValue(int deviceTypeId, int deviceTypeIndex, QString value);

protected slots:
    void clientReadyReadSlot();

protected:
    void connectToHost();
    void handleFrame(const QJsonObject &obj);
    void handleLoginReFrame(const QJsonObject &obj);
    void handleRegisterReFrame(const QJsonObject &obj);
    void handleDeviceValueFrame(const QJsonObject &obj);

    static NetMain *obj;
    QTcpSocket *client;

};

#endif // NETMAIN_H
